package com.darkness.rpc.framework.protocol.dark;

import com.darkness.rpc.framework.common.util.ProtostuffUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.io.Serializable;

/**
 * @author darkness
 * @time 2022/4/15 13:11
 * @description
 */
public class DarkProtocolEncoder extends MessageToByteEncoder<Serializable> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Serializable result, ByteBuf out) throws Exception {
        byte[] serializer = ProtostuffUtil.serializer(result);
        int length = serializer.length;
        out.writeInt(length);
        out.writeBytes(serializer);
    }
}
